<?php

namespace Store\Controller;
use Think\Controller;
class AccountController extends CommonController {
  	function _initialize(){
		parent::_initialize();
		$add_count_price=M('store_money_log')->where('type=1 and store_id='.$store_id)->sum('money');
		$del_count_price=M('store_money_log')->where('type=0 and store_id='.$store_id)->sum('money');
	
    	$store_info['money']=$add_count_price-$del_count_price;
    	$store_info['id']=session('store_id');
    	M('store')->save($store_info);
	}
    public function shopaccount(){
    	$store_id=session('store_id');
    	$count=M('store_money_log')->where('store_id='.$store_id)->count();
    	$Page = new \Think\Page ($count,20); // 实例化分页类 传入总记录数
		  $Page->setConfig('header','条记录');
	    $Page->setConfig('prev','上一页');
	    $Page->setConfig('next','下一页');
		  $show = $Page->show(); // 分页显示输出
		  $rs=M('store_money_log')->where('store_id='.$store_id)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
    	$add_count_price=M('store_money_log')->where('type=1 and store_id='.$store_id)->sum('money');
    	$del_count_price=M('store_money_log')->where('type=0 and store_id='.$store_id)->sum('money');
    	$this->assign('add_count_price',$add_count_price);
    	$this->assign('del_count_price',$del_count_price);
    	$this->assign('rs',$rs);
    	$this->assign('page',$show);
      $this->display();
    }
    
    public function tixian(){
    	$store_id=session('store_id');
    	$add_count_price=M('store_money_log')->where('type=1 and store_id='.$store_id)->sum('money');
    	$del_count_price=M('store_money_log')->where('type=0 and store_id='.$store_id)->sum('money');
    	$get_web_invoice=get_web_invoice();
		$this->assign('get_web_invoice',$get_web_invoice);
    	if($_POST){
    		$store_money_log=M('store_money_log');
    		$users_getmoney=M('users_getmoney');
    		$store_money_log->startTrans();
    		$users_getmoney->startTrans();
    		
    		$max_money=$add_count_price-$del_count_price;
    		$money=I('post.money');
    		if($money<=0){
                $this->ajax_r(1,'提现金额不能小于等于0',array('url' => U('Member/index')));
    		}
    		if($money<=$get_web_invoice['web_tixian_store']){
                $this->ajax_r(0,"提现金额不能小于店铺提现最小值");
    		}
    		
    		if($money<=(string)$max_money){
    			$log['store_id']=$store_id;
    			$log['addtime']=time();
    			$log['money']=$money;
    			$log['type']=0;
    			$log['other']='提现';
    			$log['money_type']=2;
    			$rs_log=$store_money_log->add($log);
    			if($rs_log){
    				$order_num=$this->build_order_no();
    				$getmoney['uid']=$store_id;
    				$getmoney['order_num']=$order_num;
    				$getmoney['log_id']=$rs_log;
    				$getmoney['money']=$money;
    				$getmoney['time']=time();
    				$getmoney['type']=2;
    				$rs=$users_getmoney->add($getmoney);
    				if($rs){
    					$store_money_log->commit();
    					$users_getmoney->commit();
                        $this->ajax_r(0,"提交成功，金额会在24小时内打到您的账户内.");
    				}else{
    					$store_money_log->rollback();
    					$users_getmoney->rollback();
                        $this->ajax_r(0,"提现失败");
    				}
    			}
    		}else{
                $this->ajax_r(0,"超过可提金额");
    		}
    		
    	}else{
    		$this->assign('add_count_price',$add_count_price);
    		$this->assign('del_count_price',$del_count_price);
     	  $this->display();
    	}
    	
    }
    
    public function build_order_no()
    {
        $no = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        //检测是否存在
        $db = M('users_getmoney');
        $info = $db->where(array('order_num'=>$no))->find();
        (!empty($info)) && $no = $this->build_order_no();
        return $no;
        
    }
}
